package st.strategy.vo;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.math3.stat.StatUtils;
import org.apache.commons.math3.stat.descriptive.SynchronizedDescriptiveStatistics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import st.BasicStockData;
import st.base.Sysdate;

/** 表一檔股檔的Ma */
public class Ma implements TechLine {
	public Ma(Stock stock, Collection<BasicStockData> stockDatas, int n) {
		this.stockDatas = stockDatas;
		this.stock = stock;
		this.n = n;
		this.descriptiveStatistics = new SynchronizedDescriptiveStatistics(
				this.n);
		this.data = new HashMap<Sysdate, SimpleTechValue>();
	}

	protected int n;
	protected Collection<BasicStockData> stockDatas;
	protected SynchronizedDescriptiveStatistics descriptiveStatistics;
	protected Map<Sysdate, SimpleTechValue> data;
	protected Stock stock;

	Logger logger = LoggerFactory.getLogger(getClass());

	public void build() {
		logger.debug("產生MA{},{}", n, stock.getStockId());
		for (BasicStockData stockData : stockDatas) {
			descriptiveStatistics.addValue(stockData.getClose());
			double[] values = descriptiveStatistics.getValues();
			double mean = StatUtils.mean(values);
			SimpleTechValue stv = new SimpleTechValue();
			stv.setValue(mean);
			data.put(stockData.getTradeDate(), stv);
		}
	}
	@Override
	public TechValue getValue(Sysdate date) {
		return data.get(date);
	}

}
